<?php

declare(strict_types=1);

namespace app\controller\admin;

use think\Request;
use app\middleware\JwtAuthForAdmin;
use app\model\admin\LoginState;
use app\model\AuthAdminsModel;

/**
 * 管理员 控制器
 */
class AuthAdmins
{
    // 控制器中间件 [Auth::class => ['except' => [], 'only'=> []]];
    protected $middleware = [JwtAuthForAdmin::class];

    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index(Request $request)
    {
        $groupName = $request->param('group_name', '');
        $searchText = $request->param('search_text', '');
        $pageSize = intval($request->param('page_size', 10));
        $msgInfo = AuthAdminsModel::doIndex($groupName, $searchText, $pageSize);
        return json($msgInfo);
    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
        // 表单数据处理
        $formData = $request->param();
        $msgInfo = AuthAdminsModel::doSave($formData, $request->ip());
        return json($msgInfo);
    }

    /**
     * 显示指定的资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function read($id)
    {
        if ($id == 'me') {
            $id = LoginState::getId();
        }
        $msgInfo = AuthAdminsModel::doRead($id);
        return json($msgInfo);
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update(Request $request, $id)
    {
        $msgInfo = AuthAdminsModel::doUpdate($id, $request->param());
        return json($msgInfo);
    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete($id)
    {
        $msgInfo = AuthAdminsModel::doDelete($id);
        return json($msgInfo);
    }
}
